package com.avic.rong.userOrder.activity;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebViewClient;


import com.avic.rong.BaseActivity;
import com.avic.rong.R;
import com.avic.rong.activity.no.VoicePDFActivity;
import com.avic.rong.utils.OkHttpUtils;
import com.avic.rong.utils.SDCardUtils;
import com.avic.rong.wedgit.permissionschecker.PermissionChecker;
import com.github.barteksc.pdfviewer.PDFView;


import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import wendu.dsbridge.DWebView;

public class LookFaPiaoActivity extends BaseActivity {


    @BindView(R.id.webview)
    DWebView web;

    @BindView(R.id.pdfView)
    PDFView pdfView;
    private String faPiaoUrl;

    private WebSettings settings;
    private PermissionChecker permissionChecker;
    private String[] PERMISSIONS = new String[]{
            Manifest.permission.WRITE_EXTERNAL_STORAGE
    };
    private  String savePath;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_look_fa_piao;
    }

    @Override
    protected void initView() {
        tvTitle.setText("电子发票");
        if (TextUtils.isEmpty(getIntent().getStringExtra("faPiaoUrl"))){
            finish();
        }else {
            faPiaoUrl = getIntent().getStringExtra("faPiaoUrl");
        }
        savePath = SDCardUtils.getSDCardPath() + "rong" + File.separator;
        //去寻找是否已经有了读写的权限
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED){

            //Toast.makeText(MainActivity.this,"您申请了动态权限",Toast.LENGTH_SHORT).show();
            downloadFile(faPiaoUrl,savePath);

        }else{
            //否则去请求读写权限
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},100);

        }
     /*   permissionChecker = new PermissionChecker(LookFaPiaoActivity.this);

        if (permissionChecker.isLackPermissions(PERMISSIONS)) {
            permissionChecker.requestPermissions();
        }else{

        }*/




//        http://10.2.91.25:8021/showElectronicInvoice?invId=100990
        web.loadUrl("https://www.baidu.com");
        web.setInitialScale(100);
        web.setWebChromeClient(new WebChromeClient());
        web.setWebViewClient(new WebViewClient());


        settings = web.getSettings();


        // 设置加载进来的页面自适应手机屏幕
        settings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);


        settings.setJavaScriptEnabled(true);
        settings.setSupportZoom(false);
        settings.setBuiltInZoomControls(false);
        settings.setDisplayZoomControls(false);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);

    }

    @Override
    protected void addListener() {

    }

    private void downloadFile(String path,String savePath){
        OkHttpUtils.build().download(path, savePath, new OkHttpUtils.OnDownloadListener() {
            @Override
            public void onDownloadSuccess(File file) {
//                tv_tip.setVisibility(View.GONE);

                pdfView.fromFile(file).load();
            }
            @Override
            public void onDownloading(int progress) {
//                tv_tip.setVisibility(View.VISIBLE);
//                tv_tip.setText("正在加载中...");
//                tvPage.setText("正在加载("+progress+"/100)");
            }
            @Override
            public void onDownloadFailed() {
//                Log.i("JAVA", "onDownloadFailed");
//                tvPage.setText("加载失败..");
//                tv_tip.setVisibility(View.VISIBLE);
//                tv_tip.setText("加载失败...");
//                handler.sendEmptyMessage(0);
            }
        });
    }
}
